//
//  WarmSystemViewController.m
//  LFCWarmSystem
//
//  Created by 李振文 on 16/4/25.
//  Copyright © 2016年 李福才. All rights reserved.
//

#import "WarmSystemViewController.h"
#import "iCarousel.h"
#import "UIViewController+extension.h"
#import "WarmTableViewCell.h"

#import "ChangeLanguageViewController.h"
#import "SavePhoneNumberViewController.h"
#import "FRIDCardViewController.h"
#import "FastCallViewController.h"
#import "ChangeDefanceAreaViewController.h"
#import "SwipeUserNameViewController.h"
#import "OutInDelayTimeViewController.h"
#import "WarmVolumeTimeViewController.h"
#import "DefancePSWViewController.h"
#import "AlongDefanceDelayViewController.h"

@interface WarmSystemViewController ()<iCarouselDataSource, iCarouselDelegate,UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,MFMessageComposeViewControllerDelegate>{
    iCarousel *icarousel;
    UITableView *tableViewOne;
    UITableView *tableViewTwo;
    UITableView *tableViewThree;
    UIPageControl *pageControl;
    NSArray *textArr;
    NSArray *picArr;
}

@end

@implementation WarmSystemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:NSLocalizedString(@"WarmSystem_Sample", @"报警系统") leftBarItemsImg:@[@"btn_back1.png"] rightBarItemsImg:@[@"sear.png"]];
    [self tableViewInit];
    [self iCarouselInit];
    [self uiButtonInit];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)iCarouselInit{
    icarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 64+40*WIDTHSCALE, self.view.frame.size.width, self.view.frame.size.height-64-40*WIDTHSCALE-90*WIDTHSCALE)];
    icarousel.layer.cornerRadius = 5.0;
    icarousel.backgroundColor = BACKGROUNDVIEWCOLOR;
    icarousel.type = iCarouselTypeCoverFlow;
    icarousel.delegate = self;
    icarousel.dataSource = self;
    
    [self.view addSubview:icarousel];
}
-(void)tableViewInit{
    textArr = @[NSLocalizedString(@"ChangeLanguage_Sample", @"修改主机语言"),
                NSLocalizedString(@"SavePhoneNUm_Sample", @"存储电话机短信号码"),
                NSLocalizedString(@"RFIDNum_Sample", @"RFID卡短信通知号码"),
                NSLocalizedString(@"DeleteRFID_Sample", @"删除RFID"),
                NSLocalizedString(@"FastCallPhoneNum_Sample", @"一键通号码"),
                NSLocalizedString(@"ChangeDefanceArea_Sample", @"更改防区名称"),
                NSLocalizedString(@"SwipeUser_Sample", @"刷卡用户"),
                NSLocalizedString(@"OutInDelayTime_Sample", @"出入延迟时间"),
                NSLocalizedString(@"VolumePeal_Sample", @"警号音量及鸣响时间"),
                NSLocalizedString(@"DefancePassWord_Sample", @"撤防密码"),
                NSLocalizedString(@"IndependentDelayTime_Sample", @"独立防区延迟时间")];
    picArr=@[@"icon_5_1.png",@"icon_5_2.png",@"icon_5_3.png",
             @"icon_5_4.png",@"icon_5_5.png",
             @"icon_6_1.png",@"icon_6_2.png",@"icon_6_3.png",
             @"icon_6_4.png",@"icon_6_5.png",@"icon_6_3.png"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    tableViewOne = [[UITableView alloc] initWithFrame:CGRectMake(45*WIDTHSCALE, 0, self.view.frame.size.width-90*WIDTHSCALE, self.view.frame.size.height-64-20*WIDTHSCALE-90*WIDTHSCALE)];
    tableViewOne.tableFooterView = [[UIView alloc] init];
    tableViewOne.backgroundColor = TABLEVIEWCOLOR;
    tableViewOne.layer.cornerRadius = 5.0;
//    tableViewOne.rowHeight = TABLEVIEWCELLHEIGHTBIG;
    tableViewOne.rowHeight = (tableViewOne.frame.size.height-20)/4.0;
    tableViewOne.cellLayoutMarginsFollowReadableWidth = self.view.frame.size.width-120*WIDTHSCALE;
    tableViewOne.delegate = self;
    tableViewOne.dataSource = self;
    tableViewOne.scrollEnabled=NO;
    
    tableViewTwo = [[UITableView alloc] initWithFrame:tableViewOne.frame];
    tableViewTwo.tableFooterView = [[UIView alloc] init];
    tableViewTwo.backgroundColor = TABLEVIEWCOLOR;
    tableViewTwo.layer.cornerRadius = 5.0;
    tableViewTwo.rowHeight = (tableViewTwo.frame.size.height-20)/5.0;//rowHeight = TABLEVIEWCELLHEIGHT;
    tableViewTwo.cellLayoutMarginsFollowReadableWidth = self.view.frame.size.width-120*WIDTHSCALE;
    tableViewTwo.delegate = self;
    tableViewTwo.dataSource = self;
    tableViewTwo.scrollEnabled=NO;
    
    tableViewThree = [[UITableView alloc] initWithFrame:tableViewOne.frame];
    tableViewThree.tableFooterView = [[UIView alloc] init];
    tableViewThree.backgroundColor = TABLEVIEWCOLOR;
    tableViewThree.layer.cornerRadius = 5.0;
    tableViewThree.rowHeight = (tableViewThree.frame.size.height-20)/6.0;//rowHeight = TABLEVIEWCELLHEIGHT;
    tableViewThree.cellLayoutMarginsFollowReadableWidth = self.view.frame.size.width-120*WIDTHSCALE;
    tableViewThree.delegate = self;
    tableViewThree.dataSource = self;
    tableViewThree.scrollEnabled=NO;
}

-(void)uiButtonInit{
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(60*WIDTHSCALE, icarousel.frame.origin.y+icarousel.frame.size.height+20*WIDTHSCALE, self.view.frame.size.width-120*WIDTHSCALE, 10*WIDTHSCALE)];
    pageControl.numberOfPages = icarousel.numberOfItems;
    pageControl.backgroundColor = CLEARCOLOR;
    pageControl.currentPageIndicatorTintColor = TABLEVIEWCOLOR;
    pageControl.pageIndicatorTintColor = GRAYCOLOR;
    pageControl.currentPage = icarousel.currentItemIndex;
    [self.view addSubview:pageControl];
    UIButton *btnWIFI = [UIButton buttonWithType:UIButtonTypeCustom];
    btnWIFI.frame = CGRectMake(60*WIDTHSCALE, icarousel.frame.origin.y+icarousel.frame.size.height+30*WIDTHSCALE, (self.view.frame.size.width-120*WIDTHSCALE-50*WIDTHSCALE)/2.0, TABLEVIEWCELLHEIGHT/2.0);
    [btnWIFI addTarget:self action:@selector(setWIFI:) forControlEvents:UIControlEventTouchDown];
    btnWIFI.layer.cornerRadius = btnWIFI.frame.size.height/4.0;
    [btnWIFI setTitle: @"WIFI" forState:UIControlStateNormal];
    [btnWIFI setBackgroundColor:TABLEVIEWCOLOR];
//    [self.view addSubview:btnWIFI];
    UIButton *btnCamero = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCamero.frame = CGRectMake(btnWIFI.frame.origin.x+btnWIFI.frame.size.width+50*WIDTHSCALE, btnWIFI.frame.origin.y, btnWIFI.frame.size.width, TABLEVIEWCELLHEIGHT/2.0);
    [btnCamero addTarget:self action:@selector(setCamero:) forControlEvents:UIControlEventTouchDown];
    btnCamero.layer.cornerRadius = btnWIFI.frame.size.height/4.0;
    [btnCamero setTitle: @"摄像机" forState:UIControlStateNormal];
    [btnCamero setBackgroundColor:TABLEVIEWCOLOR];
//    [self.view addSubview:btnCamero];
}
#pragma mark- iCarouselDataSource

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    return 3;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index{
    UIView *view = [UIView new];
    switch (index) {
        case 0:
            view = tableViewOne;
            break;
        case 1:
            view = tableViewTwo;
            break;
        case 2:
            view = tableViewThree;
            break;
        default:
            break;
    }
    return view;
}

#pragma mark- iCarouselDelegate
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel{
    pageControl.currentPage = carousel.currentItemIndex;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel{
    return MAINSCREENBOUNDS.size.width;
}
#pragma mark- UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:tableViewOne]) {
        return 4;
    }else if ([tableView isEqual:tableViewTwo]) {
        return 5;
    }else{
        return 6;
    }
}

//设置是否循环滑动
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return NO;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:tableViewOne]) {
        static NSString *CellIdentifier = @"WarmSystemCell";
        WarmTableViewCell *cell = (WarmTableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){
            cell = [[WarmTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
            cell.selectedBackgroundView.backgroundColor = COLOR_CELLSELECTED_BLUE;
        }
        [cell.btnLeft setHidden:YES];
        switch (indexPath.row) {
            case 0:
                [cell.btnLeftUp setTitle:NSLocalizedString(@"SystemWarm_Sample", @"系统设防") forState:UIControlStateNormal];
                [cell.btnLeftUp setBackgroundImage:[UIImage imageNamed:@"icon_open.png"] forState:UIControlStateNormal];
                [cell.btnLeftUp setBackgroundImage:[UIImage imageNamed:@"icon_open.png"] forState:UIControlStateHighlighted];
                cell.btnLeftUp.center = CGPointMake(cell.btnLeftUp.center.x, cell.center.y);
                cell.lblRight.text = NSLocalizedString(@"SetDeface_Sample",@"设防");
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_arm.png"] forState:UIControlStateNormal];
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_arm_choose.png"] forState:UIControlStateSelected];
                cell.btnRight.userInteractionEnabled = NO;
                break;
            case 1:
                [cell.btnLeftUp setTitle:NSLocalizedString(@"CommonAreaDefance",@"普通防区设防") forState:UIControlStateNormal];
                [cell.btnLeftUp setBackgroundImage:[UIImage imageNamed:@"icon_open.png"] forState:UIControlStateNormal];
                [cell.btnLeftDown setTitle:NSLocalizedString(@"HomeDefanceCancel",@"在家防区撤防") forState:UIControlStateNormal];
                [cell.btnLeftDown setBackgroundImage:[UIImage imageNamed:@"icon_close.png"] forState:UIControlStateNormal];
                cell.lblRight.text = NSLocalizedString(@"HomeDefance",@"在家设防");
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_stay.png"] forState:UIControlStateNormal];
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_stay_choose.png"] forState:UIControlStateSelected];
                cell.btnRight.userInteractionEnabled = NO;
            default:
                break;
            case 2:
                [cell.btnLeftUp setTitle:NSLocalizedString(@"SystemDefanceCancel_Sample",@"系统撤防") forState:UIControlStateNormal];
                [cell.btnLeftUp setBackgroundImage:[UIImage imageNamed:@"icon_close.png"] forState:UIControlStateNormal];
                cell.btnLeftUp.center = CGPointMake(cell.btnLeftUp.center.x, cell.center.y);
                cell.lblRight.text = NSLocalizedString(@"DefanceCancel_Sample",@"撤防");
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_disarm.png"] forState:UIControlStateNormal];
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_disarm_choose.png"] forState:UIControlStateSelected];
                cell.btnRight.userInteractionEnabled = NO;

                break;
            case 3:
                cell.lblLeft.text = NSLocalizedString(@"LeaveMassege",@"留言");
                [cell.btnLeft setImage:[UIImage imageNamed:@"icon_voice_choose.png"] forState:UIControlStateHighlighted];
                [cell.btnLeft setImage:[UIImage imageNamed:@"icon_voice.png"] forState:UIControlStateNormal];
                [cell.btnLeft setBackgroundImage:[self buttonImageFromColor:COLOR_CELLSELECTED_BLUE] forState:UIControlStateHighlighted];
                [cell.btnLeft addTarget:self action:@selector(leaveMessage:) forControlEvents:UIControlEventTouchUpInside];
                [cell.btnLeft setHidden:NO];
                [cell.lblLeft setHidden:NO];
                [cell.lineView setHidden:NO];
                cell.lblRight.text = NSLocalizedString(@"TalkBack",@"对讲");
                [cell.btnRight addTarget:self action:@selector(makeTalk:) forControlEvents:UIControlEventTouchUpInside];
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_monitor.png"] forState:UIControlStateNormal];
                [cell.btnRight setImage:[UIImage imageNamed:@"icon_monitor_click.png"] forState:UIControlStateHighlighted];
                [cell.btnRight setBackgroundImage:[self buttonImageFromColor:COLOR_CELLSELECTED_BLUE] forState:UIControlStateHighlighted];
                break;
        }
        return cell;
    }else{
        static NSString *CellIdentifier = @"WarmSystemCellT";
        UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            [cell.textLabel setTextColor:TEXTCOLOR];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
            cell.selectedBackgroundView.backgroundColor = COLOR_CELLSELECTED_BLUE;
        }
        if ([tableView isEqual:tableViewTwo]) {
            UIImage* img=[UIImage imageNamed:picArr[indexPath.row]];
            cell.textLabel.text =textArr[indexPath.row];
            cell.textLabel.font=TABLECELLTEXTFONT;
            cell.imageView.image=img;
            return cell;
        }else if ([tableView isEqual:tableViewThree]) {
            UIImage* img=[UIImage imageNamed:picArr[indexPath.row+5]];
            cell.textLabel.text =textArr[indexPath.row+5];
            cell.imageView.image=img;
            cell.textLabel.font=TABLECELLTEXTFONT;
            return cell;
        }else{
            return nil;
        }
    }
}

//- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([tableView isEqual:tableViewOne] && indexPath.row!=3) {
//        WarmTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//        cell.btnRight.selected = YES;
//    }
//    return indexPath;
//}
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([tableView isEqual:tableViewOne] && indexPath.row!=3) {
//        WarmTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//        cell.btnRight.selected = NO;
//    }
//}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0){
    if ([tableView isEqual:tableViewOne] && indexPath.row!=3) {
        WarmTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.btnRight.selected = YES;
    }
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView isEqual:tableViewOne] && indexPath.row!=3) {
        WarmTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.btnRight.selected = NO;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:tableViewOne] && indexPath.row!=3) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self showSMSPicker:[NSString stringWithFormat:@"%ld",(long)(indexPath.row+1)%3]];
    }else if ([tableView isEqual:tableViewTwo]) {
        switch (indexPath.row) {
            case 0:{
                ChangeLanguageViewController *rootVC = [[ChangeLanguageViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 1:{
                SavePhoneNumberViewController *rootVC = [[SavePhoneNumberViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 2:{
                 FRIDCardViewController *rootVC = [[FRIDCardViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 3:{
                //删除FRID
//                [self showSMSPicker:indexPath.row];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DeleteRFID_Sample",@"删除FRID") message:nil preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Sure_Sample",  @"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *alertAction){
                    [self showSMSPicker:@"22"];
                }]];
                [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel_Sample", @"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction *alertAction){

                }]];
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
            }break;
            case 4:{
                FastCallViewController *rootVC = [[FastCallViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            default:
                break;
        }
    }else if ([tableView isEqual:tableViewThree]) {
        switch (indexPath.row) {
            case 0:{
                ChangeDefanceAreaViewController *rootVC = [[ChangeDefanceAreaViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 1:{
                SwipeUserNameViewController *rootVC = [[SwipeUserNameViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 2:{
                OutInDelayTimeViewController *rootVC = [[OutInDelayTimeViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 3:{
                WarmVolumeTimeViewController *rootVC = [[WarmVolumeTimeViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 4:{
                DefancePSWViewController *rootVC = [[DefancePSWViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            case 5:{
                AlongDefanceDelayViewController *rootVC = [[AlongDefanceDelayViewController alloc] init];
                rootVC.modelPhone = _modelPhone;
                UIViewController *vc = [[UINavigationController alloc] initWithRootViewController:rootVC];
                [self presentViewController:vc animated:YES completion:^{}];
            }break;
            default:
                break;
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}




//下划线顶格
-(void)viewDidLayoutSubviews{
    if ([tableViewOne respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableViewOne setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableViewOne respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableViewOne setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([tableViewTwo respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableViewTwo setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableViewTwo respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableViewTwo setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([tableViewThree respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableViewThree setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableViewThree respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableViewThree setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}



#pragma UIButton selected
- (void)setSelected:(UIButton *)sender{
    NSLog(@"setSelected");
}
#pragma mark - UIButton Click
-(void)setWIFI:(id)sender{
    
}
-(void)setCamero:(id)sender{
    
}

-(void)selectedCell:(UIButton *)sender{

}
-(void)leaveMessage:(id)sender{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(leaveMessage:) object:nil];
    [self showSMSPicker:@"4"];
}
-(void)makeTalk:(id)sender{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(makeTalk:) object:nil];
    [self showSMSPicker:@"3"];
}
-(void)rightAction:(id)sender{
    [self showSMSPicker:@"00"];
}

#pragma mark-Send Message
-(void)showSMSPicker:(NSString *)MSGContent{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
            picker.recipients = [NSArray arrayWithObjects:_modelPhone.phoneNumber, nil];
            picker.messageComposeDelegate = self;
            picker.body = [NSString stringWithFormat:@"%@",MSGContent];
            [self presentViewController:picker animated:YES completion:^{
            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"DeviceCantMSG", @"设备没有短信功能")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"VersionProblem", @"iOS版本过低,iOS4.0以上才支持程序内发送短信") message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
        [alert show];
    }
}

#pragma mark-MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    
    switch (result)
    {
        case MessageComposeResultCancelled:
            NSLog(@"Result: SMS sending canceled");
            break;
        case MessageComposeResultSent:
            NSLog(@"Result: SMS sent");
            break;
        case MessageComposeResultFailed:{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SendMSGFail", @"短信发送失败")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
            break;
        default:
            NSLog(@"Result: SMS not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{}];
}


//通过颜色来生成一个纯色图片
- (UIImage *)buttonImageFromColor:(UIColor *)color{
    
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); return img;
}
@end
